/*
Name:System_config
Author: Terry Dong
Description: data access layer
*/
using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Reflection;
using Gammar.Digger.SystemFramework;
using Gammar.Digger.Entity;
using Gammar.Digger.IDAL;
using Gammar.Digger.Data;
namespace Gammar.Digger.DALSqlServer
{
	public partial class System_config:ISystem_config
	{
		private DbSqlServer helper;
		
		#region constructors
		/// <summary>
        /// default constructor with the defaultConn
        /// </summary>
        public System_config()
        {
			helper = new DbSqlServer(ConnectionString.SqlServer_GPriceCollect);
        }
		
		/// <summary>
        /// default constructor with the defaultConn
        /// </summary>
		public System_config(string connString)
		{
			helper = new DbSqlServer(connString);	
		}
		#endregion
		
		#region IDataBase<System_config> 

        public int Add(System_configEntity entity)
        {
           	StringBuilder strSql=new StringBuilder();
			strSql.Append("INSERT INTO System_config(");
			strSql.Append("interval,default_image_path,default_flash_path)");
			strSql.Append(" VALUES (");
			strSql.Append("@interval,@default_image_path,@default_flash_path)");
			SqlParameter[] parameters = {
					new SqlParameter("@interval",SqlDbType.Int),
					new SqlParameter("@default_image_path",SqlDbType.VarChar),
					new SqlParameter("@default_flash_path",SqlDbType.VarChar)
					};
				parameters[0].Value = entity.interval;
				parameters[1].Value = entity.default_image_path;
				parameters[2].Value = entity.default_flash_path;
				
			return helper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
		
        public int Update(System_configEntity entity)
        {
			
           StringBuilder strSql=new StringBuilder();
			strSql.Append("UPDATE System_config SET ");
				strSql.Append("interval=@interval,");
				strSql.Append("default_image_path=@default_image_path,");
				strSql.Append("default_flash_path=@default_flash_path");
		
			strSql.Append(" WHERE config_id=@config_id");
			SqlParameter[] parameters = {
					new SqlParameter("@config_id",SqlDbType.Int),
					new SqlParameter("@interval",SqlDbType.Int),
					new SqlParameter("@default_image_path",SqlDbType.VarChar),
					new SqlParameter("@default_flash_path",SqlDbType.VarChar)
					};
				parameters[0].Value = entity.config_id;
				parameters[1].Value = entity.interval;
				parameters[2].Value = entity.default_image_path;
				parameters[3].Value = entity.default_flash_path;
				
			return helper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
		
		public int UpdateByconfig_id(System_configEntity entity)
        { 
            string where=string.Format("config_id='{0}'",entity.config_id);
			entity.config_id=null;
        	return Updatepartial(entity, where);
		}

        public int Delete(string strWhere)
        {
           StringBuilder strSql=new StringBuilder();
			strSql.Append("DELETE FROM System_config ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				strSql.Append(" WHERE "+strWhere);
			}
			return helper.ExecuteNonQuery(strSql.ToString());
        }

        public System_configEntity GetEntity(int primaryKeyId)
        {
            StringBuilder strSql=new StringBuilder();
			strSql.Append("select * from System_config ");
			strSql.Append(" where config_id=@primaryKeyId");
			SqlParameter[] parameters = {
					new SqlParameter("@primaryKeyId", SqlDbType.Int)};
			parameters[0].Value = primaryKeyId;
			SqlDataReader dr = helper.ExecuteReader(strSql.ToString(), parameters) as SqlDataReader;
            if (dr.HasRows)
            {
                dr.Read();
				System_configEntity entity=new System_configEntity();
				if(!Convert.IsDBNull(dr["config_id"]))
                {
                    entity.config_id = (int)dr["config_id"];
                }
				if(!Convert.IsDBNull(dr["interval"]))
                {
                    entity.interval = (int)dr["interval"];
                }
				entity.default_image_path=dr["default_image_path"].ToString();		
				entity.default_flash_path=dr["default_flash_path"].ToString();		
				dr.Close();
            	dr.Dispose();
				return entity;
			}
			else
			{
				dr.Close();
            	dr.Dispose();
				return null;
			}
        }

        public object GetCell(string cellName, string where)
        {
            string strSql = string.Empty;
            if (string.IsNullOrEmpty(where))
            {
                strSql = string.Format("SELECT {0} FROM System_config", cellName);
            }
            else
            {
                strSql = string.Format("SELECT {0} FROM System_config Where {1}", cellName, where);
            }
            return helper.ExecuteCell(strSql);
        }

		public DataTable GetTable(string strWhere)
		{
           StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT config_id,interval,default_image_path,default_flash_path");
			strSql.Append(" FROM System_config ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" WHERE "+strWhere);
			}
			return helper.ExecuteAdapter(strSql.ToString());
		}
		
        public SortList<System_configEntity> GetList(string strWhere)
        {
            StringBuilder strSql=new StringBuilder();
			strSql.Append("select config_id,interval,default_image_path,default_flash_path");
			strSql.Append(" FROM System_config ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			SqlDataReader dr = helper.ExecuteReader(strSql.ToString()) as SqlDataReader;
			SortList<System_configEntity> list= new SortList<System_configEntity>();
			while (dr.Read())
            {
				System_configEntity entity=new System_configEntity();
				if(!Convert.IsDBNull(dr["config_id"]))
                {
                    entity.config_id = (int)dr["config_id"];
                }
				if(!Convert.IsDBNull(dr["interval"]))
                {
                    entity.interval = (int)dr["interval"];
                }
				entity.default_image_path=dr["default_image_path"].ToString();
				entity.default_flash_path=dr["default_flash_path"].ToString();
                list.Add(entity);
            }

            dr.Close();
            dr.Dispose();

            return list;
        }
		
				
		#region select,add,update the fields that you want to operate.
		
		public int AddPartial(System_configEntity entity)
        {
            int reval = 0;
            string sql = string.Empty;
            SqlParameter[] param = DataUtility.BuildAdd(entity, "System_config", ref sql);

            if (param!=null)
            {
                reval = helper.ExecuteNonQuery(sql,param);
            }
           return reval;            
            
        }
		
		public int Updatepartial(System_configEntity entity, string where)
		{
            int reval = 0;
            string sql = string.Empty;
            SqlParameter[] param = DataUtility.BuildUpdate(entity, "System_config",where, ref sql);

            if (param != null)
            {
                reval = helper.ExecuteNonQuery(sql,param);
            }
            return reval;     
		}
		
		public List<System_configEntity> GetPartial(string[] selectedFields,string where)
        {
            List<System_configEntity> list = new List<System_configEntity>();
            string sql = DataUtility.BuildSelect(selectedFields, "System_config", where);
            if (!string.IsNullOrEmpty(sql))
            {
                DbDataReader reader= helper.ExecuteReader(sql);
             
                while (reader.Read())
                {
                    System_configEntity model = new System_configEntity();
                    foreach (string field in selectedFields)
                    {
                        PropertyInfo propInfo = model.GetType().GetProperty(field);
                        propInfo.SetValue(model, reader[field], null);
                    }

                    list.Add(model);
                }

                reader.Close();
            }
            return list;
        }
		#endregion
		

        #endregion
	}
}
